<?php
class Tool_CheckURL{

	public function __construct(){
		$request	= new Net_HTTP_Request_Receiver();
		$url		= $request->get( 'url' );
		$optMethod	= array( 'HEAD', 'GET');
		$optMethod	= array_combine( $optMethod, $optMethod );
		$optMethod	= UI_HTML_Elements::Options( $optMethod, $request->get( 'method' ) );
		$follow		= $request->get( 'follow' );

		$result		= '';
		if( trim( strlen( $url ) ) ){
			if( !preg_match( "/^[a-z-]{3,10}:\/\//", $url ) )
				$url	= "http://".$url;
			if( !Alg_Validation_Predicates::isUrl( $url ) ){
				$result	= '<div class="alert alert-error">URL invalid.</div>';
			}
			else{
				$curl	= new Net_CURL( $url );
				$curl->setOption( CURLOPT_NOBODY, ( $request->get( 'method' ) === "HEAD" ) );
				$curl->setOption( CURLOPT_FOLLOWLOCATION, (bool) $follow );
				$curl->exec();
				$info	= new ADT_List_Dictionary( $curl->getInfo() );
				if( $info->get( 'http_code' ) === 0 ){
					$result	= '<div class="alert alert-error">HTTP Request failed.</div>';
				}
				else{
					$result	= require_once 'templates/result.phpt';
				}
			}
		}

		$page	= new UI_HTML_PageFrame();
		$page->addStylesheet( "//cdn.int1a.net/css/bootstrap.min.css" );
		$page->addStylesheet( "//cdn.int1a.net/css/bootstrap-responsive.min.css" );
		$page->addStylesheet( "style.css" );
		$page->addStylesheet( "//cdn.int1a.net/js/jquery/1.10.2.min.js" );
		$page->addJavaScript( "//cdn.int1a.net/js/bootstrap.min.js" );
		$page->addMetaTag( 'name', 'viewport', "width=device-width, user-scalable=no" );
		$page->addBody( require_once 'templates/main.phpt' );
		print( $page->build() );
	}
}
?>
